package com.zhixiang.core.util;

import com.alibaba.druid.pool.DruidAbstractDataSource;

import org.hibernate.SessionFactory;
import org.quartz.Scheduler;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

/**
 * 
 * @ClassName: WebUtils 
 * @Description: 获取WebApplicationContext中的bean
 * @author whw
 * @date 2015年10月17日 上午11:05:42
 *
 */
public class WebUtils {
	/**
	 * WebApplicationContext
	 */
	static WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
	
	/**
	 * 
	 * @Title: getBean 
	 * @Description: 获取WebApplicationContext中的bean
	 * @param clazz
	 * @param paramString
	 * @return    
	 * @throws
	 */
	@SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz , String paramString){
		return (T)ctx.getBean(paramString, clazz);
	}
	
	/**
	 * 
	 * @Title: getSessionFactory 
	 * @Description: 获取SessionFactory
	 * @return    
	 * @throws
	 */
	public static SessionFactory getSessionFactory(){
		return getBean(SessionFactory.class, "sessionFactory");
	}

	/**
	 * 
	 * @Title: getScheduler 
	 * @Description: 获取任务调度Scheduler
	 * @return    
	 * @throws
	 */
	public static Scheduler getScheduler(){
		return getBean(Scheduler.class, "scheduler");
	}

	/**
	 * 获取数据源
	 * @return
	 */
	public  static DruidAbstractDataSource getDataSource(){
		return getBean(DruidAbstractDataSource.class,"dataSource");
	}
}

